package processor

import (
	"encoding/json"
	"fmt"
	"mass-user/commons/message"
	"mass-user/commons/utils"
	"mass-user/server/model"
	"net"
)

type UserProcess struct {
	Conn   net.Conn
	UserId int
}

func (userProcess *UserProcess) ServerProcessLogin(msg *message.Message) error {
	logMsg := message.LoginMessage{}
	err := json.Unmarshal([]byte(msg.Data), &logMsg)
	if err != nil {
		return err
	}
	var resMsg message.Message
	resMsg.Type = message.LoginResponseMessageType
	var resData message.LoginResponseMessage
	user, err := model.GetUserDao().Login(int(logMsg.UserId), logMsg.Pwd)

	//if logMsg.UserId==100 && logMsg.Pwd == "123456"{
	if err == nil {
		// 合法
		userProcess.UserId = user.UserId
		fmt.Println(user, "登录成功！")
		userMgr.AddOnlineUser(userProcess)
		for id, _ := range userMgr.OnlineUser {
			resData.Users = append(resData.Users, id)

		}
		userProcess.NotifyOtherOnline(user.UserId)
		resData.Code = 200
	} else {
		// 不合法
		if err == model.ERR_USER_NOTEXISTS {
			resData.Code = 301
			resData.Error = err.Error()
		} else if err == model.ERR_USER_PWD {
			resData.Code = 300
			resData.Error = err.Error()
		} else {
			resData.Code = 500
			resData.Error = "服务器内部错误！"
		}

	}
	data, err := json.Marshal(resData)
	if err != nil {
		return err
	}
	resMsg.Data = string(data)

	data, err = json.Marshal(resMsg)
	if err != nil {
		return err
	}
	tf := utils.Transfer{
		Conn: userProcess.Conn,
		Buf:  [8096]byte{},
	}
	err = tf.WriterData(data)
	if err != nil {
		return err
	}
	return nil
}

func (userProcess *UserProcess) ServerProcessReg(msg *message.Message) error {
	regMsg := message.RegisterMessage{}
	err := json.Unmarshal([]byte(msg.Data), &regMsg)
	if err != nil {
		return err
	}
	var resMsg message.Message
	resMsg.Type = message.RegisterResponseMessageType
	var resData message.RegisterResponseMessage
	err = model.GetUserDao().Reg(&regMsg.User)
	if err != nil {
		if err == model.ERR_USER_EXISTS {
			resData.Code = 203 // 已经存在
			resData.Error = err.Error()
		} else {
			resData.Code = 500
			resData.Error = err.Error()
		}

	} else {
		resData.Code = 200
	}
	data, err := json.Marshal(resData)
	if err != nil {
		return err
	}

	resMsg.Data = string(data)

	data, err = json.Marshal(resMsg)
	if err != nil {
		return err
	}
	tf := utils.Transfer{
		Conn: userProcess.Conn,
		Buf:  [8096]byte{},
	}
	err = tf.WriterData(data)
	if err != nil {
		return err
	}
	return nil
}

func (userProcess *UserProcess) NotifyOtherOnline(userId int) {
	for k, up := range userMgr.OnlineUser {
		if k == userId {
			continue
		}
		fmt.Printf("给%v发送%v上线了。。\n", k, userId)
		up.NotifyMeToOtherOnline(userId)
	}
}

func (userProcess *UserProcess) NotifyMeToOtherOnline(userId int) {
	var msg  message.Message
	msg.Type = message.NotifyUserStatusMesType
	var nuser message.NotifyUserStatusMes
	nuser.UserId = userId
	nuser.Status = message.UserOnline

	data, err := json.Marshal(nuser)
	if err != nil {
		return
	}
	msg.Data = string(data)

	data, err = json.Marshal(msg)
	if err != nil {
		return
	}

	tf := &utils.Transfer{
		Conn: userProcess.Conn,
		Buf:  [8096]byte{},
	}
	err = tf.WriterData(data)
	if err != nil {
		fmt.Println("通知上线出错 err = ", err)
		return
	}
}
